<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 < 751707632@qq.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Session;
use think\Validate;

class AdminOauthController extends AdminBaseController
{

    /**
     * 后台知鸟用户列表
     * @adminMenu(
     *     'name'   => '知鸟用户',
     *     'parent' => 'user/AdminIndex/default',
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if ( request()->isPost() ) {
            $keyword = trim($param['keyword']);
            if ( $keyword == '' || $keyword == null) {
            } else {
                $where['staff_number|staff_name|staff_duty|staff_deptname']  = ['like','%'.$keyword.'%'];
                $this->assign('keyword', $keyword);
            }
            Session::set('userList',$where);
            Session::set('userListKeyword', $keyword);
;        } else {
            if ( isset($param['page']) ) {
                $where = Session::get('userList');
                $keyword = Session::get('userListKeyword');
                if ( $keyword != null || $keyword != '' ) {
                    $this->assign('keyword', $keyword);
                }
            } else {
                Session::set('userList', null);
                Session::set('userListKeyword', null);
            }
        }

        $oauthUserQuery = Db::name('ims_member');
        $lists = $oauthUserQuery->field('id,staff_number,staff_name,staff_pic,staff_deptname,staff_duty,integral,learn_time,vedio_score,spectator,focus_amount,fans_amount,login_time,status')->where($where)->order("id desc")->paginate(10);

        $this->assign([
            'lists'         => $lists,
            'page'          => $lists->render(), // 获取分页显示
            'count'         => $oauthUserQuery->where($where)->count(), // 总数
        ]);
        return $this->fetch();
    }

    /**
     * 修改用户状态
     */
    public function update()
    {
        $request = $this->request;
        $id = input('param.id', 0, 'intval');
        $status = input('param.status', 0, 'intval');
        if (empty($id) || empty($status)) {
            $this->error('非法数据！');
        }
        if ( $status == 1 ) {
            Db::name("ims_member")->where("id", $id)->update(['status' => '2']); // 禁用
        } else {
            Db::name("ims_member")->where("id", $id)->update(['status' => '1']); // 启用
        }
        $this->success("操作成功！", "admin_oauth/index");
    }

    /**
     * 更新用户信息
     */
    public function updateAll()
    {
        // 请求全部员工接口
        $user = json_decode($this->post(STAFF_USER_ALL_USER, staffTokenDataALL()));
        if ( !$user ) $this->error('请求数据失败');
        foreach ( $user as $key => $val ) {
            // 判断用户信息是否存在，存在更新，不存在增加
            $dataSave['staff_number']   = $val->user_id;
            $dataSave['staff_name']     = $val->user_name;
            $dataSave['staff_duty']     = $val->user_duty;
            $dataSave['staff_pic']      = $val->user_picture;
            $dataSave['staff_deptname'] = $val->deptname;
            $dataSave['staff_deptid']   = $val->deptid;
            if ( isset($dataSave['staff_number']) && $dataSave['staff_number'] != '' && $dataSave['staff_number'] != null ) {  
                if ( Db::name('ims_member')->where('staff_number',$val->user_id)->find() ) {
                    $dataSave['update_time']     = date('Y-m-d H:i:s', time());
                    Db::name('ims_member')->where('staff_number', $val->user_id)->update($dataSave); // 更新数据
                } else {
                    $dataSave['create_time']     = date('Y-m-d H:i:s', time());
                    Db::name('ims_member')->insert($dataSave); // 添加数据
                }
            }
        }
        $this->success("更新完成！", "admin_oauth/index");
    }

    /***************************************************/

    /**
     * POST请求
     */
    public function post($url, $data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /***************************************************/

    /**
     * [dutyList 职称配置]
     */
    public function dutyList()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别

        $list = Db::name('ims_member_duty')->select()->toArray();
        $this->assign([
            'list'  => $list,
        ]);
        return $this->fetch();
    }

    /**
     * [dutyForm 职称添加、编辑]
     */
    public function dutyForm()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $param = request()->param();
        if ( request()->isPost() ) {
            // 数据验证
            $rule = [
                'is_straming' => 'require',
                'is_meeting'  => 'require',
                'duty'   => 'require',
            ];
            $validate = new Validate($rule);
            if(!$validate->check($param)){
                $this->error($validate->getError());exit;
            }

            $data = $param;
            if ( empty($param['id']) ) {
                // 判断职称是否存在
                if ( Db::name('ims_member_duty')->where('duty', $param['duty'])->find() ) {
                    $this->error('该职称已存在！');exit;
                }
                $data['create_time'] = date("Y-m-d H:i:s", time());
                Db::name('ims_member_duty')->insert($data);
                $this->success('添加成功！', 'AdminOauth/dutyList');exit;
            } else {
                $data['update_time'] = date("Y-m-d H:i:s", time());
                Db::name('ims_member_duty')->update($data);
                $this->success('修改成功！', 'AdminOauth/dutyList');exit;
            }
            $this->error('操作失败！');exit;
        } else {
            if ( !empty($param['id']) ) {
                $data = Db::name('ims_member_duty')->where('id', $param['id'])->field('id, duty, is_meeting, is_straming')->find();
                $this->assign('data', $data);
            }
            return $this->fetch();
        }
    }

    /**
     * [dutyDelete 删除职称]
     */
    public function dutyDelete()
    {
        $param = request()->param();
        if ( empty($param['id']) ) {
            $this->error('缺少参数');exit;
        }

        if ( Db::name('ims_member_duty')->delete($param['id']) ) {
            $this->success('删除成功');exit;
        } else {
            $this->error('删除失败');exit;
        }
    }

}